package com.dimension.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dimension.pojo.User;
import com.dimension.pojo.UserExample;
import com.dimension.service.impl.UserServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/list")
    public ModelAndView userList(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("bacUser/userLists");
        return mv;
    }



    @GetMapping("/userdata")
    @ResponseBody
    public String list(){//查询数据
        List<User> users = userService.selectByExample(new UserExample());
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",users.size());
        map.put("data",users);
        /*WriteNullStringAsEmpty*/
        String temp = JSON.toJSONString(map,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
                );
        return temp;
    }

    @GetMapping("/addUser")
    public  ModelAndView addUserPage(){
        ModelAndView view = new ModelAndView("bacUser/addUser");
        return  view;
    }



}